package com.example.demo.utils;

import java.io.IOException;

import com.example.demo.vo.RTDData;
import com.example.demo.vo.RTDataSection;
import com.example.demo.vo.RTDataSet;
import com.example.demo.vo.RTDataValue;
import com.google.gson.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;


public class JsonOperator{
    @Autowired
    private RestTemplate template;

    public static Double getWTState(String jsonStr){
        JsonParser js = new JsonParser();
        JsonObject jsonObject = (JsonObject) js.parse(jsonStr);
        try {
            String value = new Gson().fromJson(jsonObject.getAsJsonArray("RTDataSets").get(0).getAsJsonObject().getAsJsonArray("RTDataValues").get(0).getAsJsonObject().get("Value"),String.class);
            return Double.valueOf(value).doubleValue();
        } catch (Exception e){
            return  0.0;
        }

    }

    public static int[] getStateValue(String jsonStr){
        JsonParser js = new JsonParser();
        Gson gson = new Gson();
        JsonObject jo = js.parse(jsonStr).getAsJsonObject();
        JsonArray ja = jo.getAsJsonArray("RTDataSets");
        int a[] = new int[ja.size()];
        try {
            for (int i = 0; i<ja.size(); i++){
                JsonObject jsonObject = ja.get(i).getAsJsonObject();
                RTDataSet temp = gson.fromJson(jsonObject,RTDataSet.class);
                RTDataValue tempValue[] = new RTDataValue[temp.getRTDataValues().size()];
                tempValue = temp.getRTDataValues().toArray(tempValue);
                a[i] = (int) Double.parseDouble(tempValue[0].getValue());
            }
            return a;
        }
        catch (Exception e ){
            return a;
        }
    }
    /*
    * 2017.6.24 yangge
    * 通用的KKS码返回字段的解析，解析之后的层次结构为：
    * RTData->RTDataSetNew[]->RTDataValueNew[]
    * 使用的是JsonIterator解析json，比gson快很多
    * */
    public static RTDData getRTDateByJsonString(String jsonStr) throws IOException {
//            JsonIterator jsonIterator=JsonIterator.parse(jsonStr);
//            RTDData rtdData=jsonIterator.read(RTDData.class);
        RTDData rtdData=new Gson().fromJson(jsonStr,RTDData.class);

        return rtdData;
    }
    public static RTDataSection getRTDataSectionByJsonString(String jsonStr) throws IOException {
        //System.out.println(jsonStr);
        return new Gson().fromJson(jsonStr,RTDataSection.class);
    }

    public static void main(String[] args) throws IOException {

        String jsonStr="{\"BasicResponse\":{\"succeeded\":1},\"RTDataSets\":[{\"tag\":\"NM002W03MZZ00000KA002AF08\",\"kksCode\":\"NM002W03MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":35257.67}]},{\"tag\":\"NM002W39MZZ00000KA002AF08\",\"kksCode\":\"NM002W39MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":31853.48}]},{\"tag\":\"NM002W02MZZ00000KA002AF08\",\"kksCode\":\"NM002W02MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":34337.13}]},{\"tag\":\"NM002W33MZZ00000KA002AF08\",\"kksCode\":\"NM002W33MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":35608.15}]},{\"tag\":\"NM002W13MZZ00000KA002AF08\",\"kksCode\":\"NM002W13MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":31557.65}]},{\"tag\":\"NM002W38MZZ00000KA002AF08\",\"kksCode\":\"NM002W38MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":19992.51}]},{\"tag\":\"NM002W29MZZ00000KA002AF08\",\"kksCode\":\"NM002W29MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":31726.82}]},{\"tag\":\"NM002W01MZZ00000KA002AF08\",\"kksCode\":\"NM002W01MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":32114.16}]},{\"tag\":\"NM002W11MZZ00000KA002AF08\",\"kksCode\":\"NM002W11MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":30379.91}]},{\"tag\":\"NM002W14MZZ00000KA002AF08\",\"kksCode\":\"NM002W14MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":32722.68}]},{\"tag\":\"NM002W41MZZ00000KA002AF08\",\"kksCode\":\"NM002W41MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":27323.22}]},{\"tag\":\"NM002W15MZZ00000KA002AF08\",\"kksCode\":\"NM002W15MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":31601.0}]},{\"tag\":\"NM002W47MZZ00000KA002AF08\",\"kksCode\":\"NM002W47MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":31390.87}]},{\"tag\":\"NM002W25MZZ00000KA002AF08\",\"kksCode\":\"NM002W25MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":25748.48}]},{\"tag\":\"NM002W40MZZ00000KA002AF08\",\"kksCode\":\"NM002W40MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":34542.24}]},{\"tag\":\"NM002W09MZZ00000KA002AF08\",\"kksCode\":\"NM002W09MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":31690.81}]},{\"tag\":\"NM002W37MZZ00000KA002AF08\",\"kksCode\":\"NM002W37MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":27489.37}]},{\"tag\":\"NM002W06MZZ00000KA002AF08\",\"kksCode\":\"NM002W06MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":33414.36}]},{\"tag\":\"NM002W42MZZ00000KA002AF08\",\"kksCode\":\"NM002W42MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":17129.73}]},{\"tag\":\"NM002W34MZZ00000KA002AF08\",\"kksCode\":\"NM002W34MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":29660.24}]},{\"tag\":\"NM002W16MZZ00000KA002AF08\",\"kksCode\":\"NM002W16MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":31630.42}]},{\"tag\":\"NM002W08MZZ00000KA002AF08\",\"kksCode\":\"NM002W08MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":30765.1}]},{\"tag\":\"NM002W30MZZ00000KA002AF08\",\"kksCode\":\"NM002W30MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":28880.27}]},{\"tag\":\"NM002W49MZZ00000KA002AF08\",\"kksCode\":\"NM002W49MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":26490.65}]},{\"tag\":\"NM002W26MZZ00000KA002AF08\",\"kksCode\":\"NM002W26MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":30992.83}]},{\"tag\":\"NM002W10MZZ00000KA002AF08\",\"kksCode\":\"NM002W10MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":9766.36}]},{\"tag\":\"NM002W35MZZ00000KA002AF08\",\"kksCode\":\"NM002W35MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":19931.32}]},{\"tag\":\"NM002W48MZZ00000KA002AF08\",\"kksCode\":\"NM002W48MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":32095.92}]},{\"tag\":\"NM002W17MZZ00000KA002AF08\",\"kksCode\":\"NM002W17MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":31659.35}]},{\"tag\":\"NM002W46MZZ00000KA002AF08\",\"kksCode\":\"NM002W46MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":31187.0}]},{\"tag\":\"NM002W43MZZ00000KA002AF08\",\"kksCode\":\"NM002W43MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":28976.5}]},{\"tag\":\"NM002W22MZZ00000KA002AF08\",\"kksCode\":\"NM002W22MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":30989.29}]},{\"tag\":\"NM002W45MZZ00000KA002AF08\",\"kksCode\":\"NM002W45MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":27015.68}]},{\"tag\":\"NM002W36MZZ00000KA002AF08\",\"kksCode\":\"NM002W36MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":13736.05}]},{\"tag\":\"NM002W07MZZ00000KA002AF08\",\"kksCode\":\"NM002W07MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":25718.55}]},{\"tag\":\"NM002W31MZZ00000KA002AF08\",\"kksCode\":\"NM002W31MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":0.0}]},{\"tag\":\"NM002W21MZZ00000KA002AF08\",\"kksCode\":\"NM002W21MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":29130.57}]},{\"tag\":\"NM002W12MZZ00000KA002AF08\",\"kksCode\":\"NM002W12MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":26404.48}]},{\"tag\":\"NM002W19MZZ00000KA002AF08\",\"kksCode\":\"NM002W19MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":29819.17}]},{\"tag\":\"NM002W20MZZ00000KA002AF08\",\"kksCode\":\"NM002W20MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":28619.9}]},{\"tag\":\"NM002W27MZZ00000KA002AF08\",\"kksCode\":\"NM002W27MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":29481.72}]},{\"tag\":\"NM002W05MZZ00000KA002AF08\",\"kksCode\":\"NM002W05MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":33074.27}]},{\"tag\":\"NM002W24MZZ00000KA002AF08\",\"kksCode\":\"NM002W24MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":28132.0}]},{\"tag\":\"NM002W32MZZ00000KA002AF08\",\"kksCode\":\"NM002W32MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":33918.66}]},{\"tag\":\"NM002W28MZZ00000KA002AF08\",\"kksCode\":\"NM002W28MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":29085.75}]},{\"tag\":\"NM002W18MZZ00000KA002AF08\",\"kksCode\":\"NM002W18MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":21782.32}]},{\"tag\":\"NM002W23MZZ00000KA002AF08\",\"kksCode\":\"NM002W23MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":30116.31}]},{\"tag\":\"NM002W44MZZ00000KA002AF08\",\"kksCode\":\"NM002W44MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":30506.52}]},{\"tag\":\"NM002W04MZZ00000KA002AF08\",\"kksCode\":\"NM002W04MZZ00000KA002AF08\",\"RTDataValues\":[{\"Time\":1497718800000,\"Value\":0.0}]}]}";
//        String jsonStr="{\"BasicResponse\":{\"succeeded\":1},\"RTDataSets\":[{\"tag\":\"NM301WS7QZA000A1DB001AA04\",\"kksCode\":\"NM301WS7QZA000A1DB001AA04\",\"RTDataValues\":[{\"Time\":1491094500000,\"Value\":10.0},{\"Time\":1491095400000,\"Value\":1.0},{\"Time\":1491095700000,\"Value\":7.0},{\"Time\":1491104400000,\"Value\":7.0},{\"Time\":1491107100000,\"Value\":7.0},{\"Time\":1491107400000,\"Value\":5.0},{\"Time\":1491107700000,\"Value\":7.0},{\"Time\":1491108000000,\"Value\":5.0},{\"Time\":1491108300000,\"Value\":7.0},{\"Time\":1491108600000,\"Value\":5.0},{\"Time\":1491108900000,\"Value\":7.0},{\"Time\":1491110100000,\"Value\":7.0},{\"Time\":1491112200000,\"Value\":5.0},{\"Time\":1491112500000,\"Value\":5.0},{\"Time\":1491112800000,\"Value\":4.0},{\"Time\":1491113100000,\"Value\":7.0},{\"Time\":1491113400000,\"Value\":5.0},{\"Time\":1491113700000,\"Value\":4.0},{\"Time\":1491114000000,\"Value\":7.0},{\"Time\":1491116100000,\"Value\":7.0},{\"Time\":1491144000000,\"Value\":7.0},{\"Time\":1491167700000,\"Value\":4.0},{\"Time\":1491185400000,\"Value\":10.0},{\"Time\":1491185700000,\"Value\":4.0}]}]}";
//       String jsonStr="{\"BasicResponse\":{\"succeeded\":1},\"RTDataSets\":[{\"tag\":\"NM301WS7QZA000A1DB001AA04\",\"kksCode\":\"NM301WS7QZA000A1DB001AA04\",\"RTDataValues\":[{\"Time\":1491094500000,\"Value\":10.0},{\"Time\":1491095400000,\"Value\":1.0},{\"Time\":1491095700000,\"Value\":7.0},{\"Time\":1491104400000,\"Value\":7.0},{\"Time\":1491107100000,\"Value\":7.0},{\"Time\":1491107400000,\"Value\":5.0},{\"Time\":1491107700000,\"Value\":7.0},{\"Time\":1491108000000,\"Value\":5.0},{\"Time\":1491108300000,\"Value\":7.0},{\"Time\":1491108600000,\"Value\":5.0},{\"Time\":1491108900000,\"Value\":7.0},{\"Time\":1491110100000,\"Value\":7.0},{\"Time\":1491112200000,\"Value\":5.0},{\"Time\":1491112500000,\"Value\":5.0},{\"Time\":1491112800000,\"Value\":4.0},{\"Time\":1491113100000,\"Value\":7.0},{\"Time\":1491113400000,\"Value\":5.0},{\"Time\":1491113700000,\"Value\":4.0},{\"Time\":1491114000000,\"Value\":7.0},{\"Time\":1491116100000,\"Value\":7.0},{\"Time\":1491144000000,\"Value\":7.0},{\"Time\":1491167700000,\"Value\":4.0},{\"Time\":1491185400000,\"Value\":10.0},{\"Time\":1491185700000,\"Value\":4.0}]},{\"tag\":\"NM301WS7QZA000A1DB001AA04\",\"kksCode\":\"NM301WS7QZA000A1DB001AA04\",\"RTDataValues\":[{\"Time\":1491094500000,\"Value\":10.0},{\"Time\":1491095400000,\"Value\":1.0},{\"Time\":1491095700000,\"Value\":7.0},{\"Time\":1491104400000,\"Value\":7.0},{\"Time\":1491107100000,\"Value\":7.0},{\"Time\":1491107400000,\"Value\":5.0},{\"Time\":1491107700000,\"Value\":7.0},{\"Time\":1491108000000,\"Value\":5.0},{\"Time\":1491108300000,\"Value\":7.0},{\"Time\":1491108600000,\"Value\":5.0},{\"Time\":1491108900000,\"Value\":7.0},{\"Time\":1491110100000,\"Value\":7.0},{\"Time\":1491112200000,\"Value\":5.0},{\"Time\":1491112500000,\"Value\":5.0},{\"Time\":1491112800000,\"Value\":4.0},{\"Time\":1491113100000,\"Value\":7.0},{\"Time\":1491113400000,\"Value\":5.0},{\"Time\":1491113700000,\"Value\":4.0},{\"Time\":1491114000000,\"Value\":7.0},{\"Time\":1491116100000,\"Value\":7.0},{\"Time\":1491144000000,\"Value\":7.0},{\"Time\":1491167700000,\"Value\":4.0},{\"Time\":1491185400000,\"Value\":10.0},{\"Time\":1491185700000,\"Value\":4.0}]}]}";
        RTDData data=getRTDateByJsonString(jsonStr);
        System.out.println(data);


    }

}